package com.stox.core.model;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import com.stox.core.model.exception.StoxException;
import com.stox.core.util.Constant;
import com.stox.core.util.StringUtil;
import com.stox.resources.i18n.MessageSource;



public class Exchange implements Comparable<Exchange>{

	private String code;
	private String name;
	private Date lastTradingDate;
	private String country;
	private String flagImage;
	private boolean firstTimeDownloadCompleted;
	private boolean configured;
	
	
	public int compareTo(Exchange o) {
		if(o != null && o.getName() != null && getName() != null){
			return getName().compareToIgnoreCase(o.getName());
		}
		return 0;
	}
	
	public void validate(){
		if(!StringUtil.hasText(getCode())){
			throw new StoxException(MessageSource.getMessage("Exchange.code.is.null.or.empty"));
		}
		if(!StringUtil.hasText(getName())){
			throw new StoxException(MessageSource.getMessage("Exchange.name.is.null.or.empty"));
		}
	}
	
	public void format(){
		validate();
		setCode(getCode().trim().toUpperCase());
		setName(getName().trim());
	}
	
	public static Exchange fromString(String string){
		Exchange exchange = null;
		if(StringUtil.hasText(string)){
			exchange = new Exchange();
			String tokens[] = string.split(",");
			exchange.setCode(tokens[0]);
			exchange.setName(tokens[1]);
			try {
				exchange.setLastTradingDate(Constant.dateFormat.parse(tokens[2]));
			} catch (ParseException e) {
				throw new StoxException(e);
			}
			exchange.setCountry(tokens[3]);
			exchange.setFlagImage(tokens[4]);
			exchange.setFirstTimeDownloadCompleted(Boolean.parseBoolean(tokens[5]));
			exchange.setConfigured(Boolean.parseBoolean(tokens[6]));
		}
		return exchange;
	}
	
	@Override
	public String toString() {
		return getName()+", "+getCountry();
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Exchange other = (Exchange) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public Date getLastTradingDate() {
		if(lastTradingDate == null){
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.YEAR, -3);
			lastTradingDate = calendar.getTime();
		}
		return lastTradingDate;
	}

	public void setLastTradingDate(Date lastTradingDate) {
		this.lastTradingDate = lastTradingDate;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getFlagImage() {
		return flagImage;
	}

	public void setFlagImage(String flagImage) {
		this.flagImage = flagImage;
	}

	public boolean isFirstTimeDownloadCompleted() {
		return firstTimeDownloadCompleted;
	}

	public void setFirstTimeDownloadCompleted(boolean firstTimeDownloadCompleted) {
		this.firstTimeDownloadCompleted = firstTimeDownloadCompleted;
	}

	public boolean isConfigured() {
		return configured;
	}

	public void setConfigured(boolean configured) {
		this.configured = configured;
	}

}
